package com.zscat.mallplus.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zscat.mallplus.annotation.IgnoreAuth;
import com.zscat.mallplus.annotation.SysLog;
import com.zscat.mallplus.dms.entity.DmsOrder;
import com.zscat.mallplus.dms.service.*;
import com.zscat.mallplus.enums.DeviceStatus;
import com.zscat.mallplus.enums.RoomStatus;
import com.zscat.mallplus.qsg.*;
import com.zscat.mallplus.utils.CommonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * 积分管理Controller
 */
@RestController
@Api(tags = "QsgIntegralController", description = "积分相关管理")
@RequestMapping("/api/dms/integral")
public class QsgIntegralController {

    @Autowired
    private IntegralService integralService;
    @Autowired
    private IQsgIntegralDetailService integralDetailService;
    @Autowired
    private IDmsOrderService orderService;
    @Autowired
    private IQsgRechargeOrderService rechargeOrderService;

    @ApiOperation("查询积分")
    @RequestMapping(value = "/getIntegral")
    @ResponseBody
    public Object getIntegral(Long memberId,Long storeId) {
        try {
            QueryWrapper qw = new QueryWrapper();
            qw.eq("member_id",memberId);
            qw.eq("store_id",storeId);
            QsgIntegral integral = integralService.getOne(qw);
            if (null!=integral){
                return new CommonResult().success(integral);
            }else {
                integral = new QsgIntegral();
                return new CommonResult().success(integral);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new CommonResult().failed();
        }
    }

    @ApiOperation("添加积分")
    @RequestMapping(value = "/addJifen",method = RequestMethod.POST)
    @ResponseBody
    public Object addJifen(@RequestBody QsgIntegral entity) {
        try {
            QueryWrapper qw = new QueryWrapper();
            qw.eq("store_id",entity.getStoreId());
            qw.eq("member_id",entity.getMemberId());
            QsgIntegral integral = integralService.getOne(qw);
            if (null==integral){
                integral = new QsgIntegral();
                integral.setIntegral(entity.getIntegral());
                integral.setStoreId(entity.getStoreId());
                integral.setMemberId(entity.getMemberId());
                integral.setCreateDatetime(new Date());
                integralService.save(integral);
            }else {
                integral.setIntegral(entity.getIntegral());
                integralService.updateById(integral);
            }
            return new CommonResult().success(integral);
        } catch (Exception e) {
            e.printStackTrace();
            return new CommonResult().failed();
        }
    }

    @ApiOperation("查询会员积分明细")
    @RequestMapping(value = "/getIntegralDetail")
    @ResponseBody
    public Object getIntegralDetail(Long memberId,Long storeId) {
        try {
            QueryWrapper qw = new QueryWrapper();
            qw.eq("member_id",memberId);
            qw.eq("store_id",storeId);
            qw.orderByDesc("create_datetime");
            List<QsgIntegralDetail> detailList = integralDetailService.list(qw);
            if (null!=detailList&&detailList.size()>0){
                for (QsgIntegralDetail detail : detailList){
                    if (detail.getTypeName().equals("积分充值")){
                        QsgRechargeOrder order = rechargeOrderService.getById(detail.getOrderId());
                        if (null!=order){
                            detail.setRechargeOrder(order);
                        }
                    }else{
                        DmsOrder order = orderService.getById(detail.getOrderId());
                        if (null!=order){
                            detail.setOrder(order);
                        }
                    }
                }
            }
            return new CommonResult().success(detailList);//个人在个店的积分充值和消费明细
        } catch (Exception e) {
            e.printStackTrace();
            return new CommonResult().failed();
        }
    }
}
